//注册界面逻辑功能


var validateCodeCD = 60;//获取验证码冷却时间
var myCDcounter;//验证码冷却计时器

//发送验证码按钮-发送验证码，启动计时器
function onSendValidateCodeClicked(){

    if(!isPhoneValid)
    {
        console.log("NONONO")
        return;
    }
    //异步请求验证码
    var phone = $("#phoneNumber").val();
    $.ajax({
        url:"/sms/sendCodeAgain",
        type:"POST",
        data:{phone:phone},

        success: function(data){
            console.log("ask validateCode success");
            //启动计时器，修改按钮样式
            validateCodeCD = 60;
            $("#getValidateCode").attr("disabled","disabled");//禁用按钮
            myCDcounter = setInterval(countTime,1000);
        }
    });

}

//计时器事件函数
function countTime(){
    validateCodeCD--;
    $("#getValidateCode").text(validateCodeCD+"秒后再次获取");
    if(validateCodeCD === 0){
        $("#getValidateCode").text("获取验证码");
        $("#getValidateCode").removeAttr("disabled");//复启用按钮
        clearInterval(myCDcounter);
    }
}

//全局变量区域
var username=document.getElementById("username");//用户名
var password=document.getElementById("password");//密码
var repassword=document.getElementById("repassword");//重复密码
var phone=document.getElementById("phoneNumber");//手机号码
var tips=document.getElementById("tips");//提示信息
var xhr='';//xhr对象
var isUsernameValid=false;
var isPasswordValid=false;
var isPhoneValid=false;
var formlabel=document.getElementById('signUp-from');//表单
//****

//添加监听器区域
console.log(username);
username.addEventListener("blur",checkUsernameValid,false);
password.addEventListener("blur",checkPasswordValid,false);
repassword.addEventListener("blur",checkPasswordValid,false);
phone.addEventListener("blur",checkPhoneValid,false);
//****

function checkPhoneValid(){
    if (phone.value == '' || phone.value == null || phone.value ==undefined){
        tips.innerHTML='';
        tips.innerHTML+='<h4 style="line-height:25px ;color: red;">手机号不能为空!</h4>';
        isPhoneValid=false;
        return isPhoneValid;
    }
    else {
        checkPhoneIsExits(phone.value)
        // return isPhoneValid;
    }

}

function checkPhoneIsExits(userPhone){
    xhr=new XMLHttpRequest();
    xhr.open('GET','phoneExit?phone='+userPhone);
    xhr.send(null);
    xhr.onreadystatechange = phoneExitProcess;
}

function phoneExitProcess(){
    if(xhr.readyState === 4) {
        if(xhr.status === 200) {
            var responseText = xhr.responseText;
            if(responseText==="no"){
                isPhoneValid=true;
                tips.innerHTML='欢迎注册！';
            }
            else{
                isPhoneValid=false;
                tips.innerHTML='';
                tips.innerHTML+='<h4 style="line-height:25px ;color: red;">手机号已经存在!</h4>';
            }
        }
    }
    return isPhoneValid;
}


//检查用户名是否合法：先判空，不为空的话判是否存在
function checkUsernameValid(){
    if(username.value == '' || username.value==null || username.value==undefined){
        tips.innerHTML='';
        tips.innerHTML+='<h4 style="line-height:25px ;color: red;">用户名不能为空!</h4>';
        isUsernameValid=false;
        return isUsernameValid;

    }else{
        // tips.innerHTML='';
        // tips.innerHTML+='<h4 style="line-height:25px ;">欢迎注册!</h4>';
        checkIsExit(username.value);
        return isUsernameValid;
    }

}

//异步请求验证用户名是否存在
function checkIsExit(usernameStr){
    xhr=new XMLHttpRequest();
    xhr.onreadystatechange = usernameExitProcess;
    xhr.open('GET','usernameExit?username='+usernameStr);
    xhr.send(null);
}

function usernameExitProcess(){
    if(xhr.readyState === 4) {
        if(xhr.status === 200) {
            var responseText = xhr.responseText;
            if(responseText==="no"){
                isUsernameValid=true;
                if(checkPasswordValid())//检查密码状态是否合法
                {
                    tips.innerHTML = '';
                    tips.innerHTML += '<h4 style="line-height:25px ;">欢迎注册!</h4>';
                }

            }else{
                isUsernameValid=false;
                tips.innerHTML='';
                tips.innerHTML+='<h4 style="line-height:25px ;color: red;">用户名已经存在!</h4>';
            }
        }
    }
}
//****

//检查密码是否合法
function checkPasswordValid(){
    if(isUsernameValid === false)//确保先检验用户名
    {
        return ;
    }
    if(password.value === ""||repassword.value === "")//如果密码栏有未填写，判断状态合法
    {
        return true;
    }
    if(password.value==='' || password.value===null || password.value===undefined){
        tips.innerHTML='';
        tips.innerHTML+='<h4 style="line-height:25px ;color: red;">密码不能为空!</h4>';
        isPasswordValid=false;
        return isPasswordValid;

    }else if(repassword.value!==password.value&&repassword.value!=='' && repassword.value!==null){
        tips.innerHTML='';
        tips.innerHTML+='<h4 style="line-height:25px ;color: red;">两次输入的密码不一致!</h4>';
        isPasswordValid=false;
        return isPasswordValid;

    }
    tips.innerHTML='';
    tips.innerHTML+='<h4 style="line-height:25px ;">欢迎注册!</h4>';

    isPasswordValid=true
    return isPasswordValid;
}

var login_btn1 = document.querySelector("#btn-signup")
login_btn1.addEventListener('click',login1);
function login1(){
    var validateCode = $("#validateCode").val();
    $.ajax(
        {
            type: 'GET',
            url: 'signUp?validateCode='+validateCode,
            data: validateCode,
            processData: false,
            contentType: false,
            success: function (data)
            {
                if(data === "invalid code"){
                    tips.innerHTML+='<h4 style="line-height:25px ;">验证码错误</h4>';
                }
            },
            error: function (errorMsg)
            {
                console.log("fail")
                console.log(errorMsg);
            },
        }
    )
}

//点击注册按钮
function onSignUpClick(){
    if(isUsernameValid && isPasswordValid && isPhoneValid ){
        formlabel.submit();
    }
}



//显示信息框
function showAlert(alertId){
    $("#"+alertId).removeClass("invisible");
}

//隐藏信息框
function hideAlert(alertId){
    $("#"+alertId).addClass("invisible");
}